package com.qys.livesMall.app.user.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qys.livesMall.app.user.model.entity.UserStatisticsDayLog;
import com.qys.livesMall.app.user.model.entity.UserStatisticsInfo;
import com.qys.livesMall.app.user.scheduledTask.UserStatisticsInfoScheduledTask;
import com.qys.livesMall.app.user.service.UserStatisticsDayLogService;
import com.qys.livesMall.app.user.service.UserStatisticsInfoService;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.finance.model.dto.user.UserIncomeDayDTO;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 今日店铺和用户统计表 前端控制器
 * </p>
 *
 * @author 
 * @since 2023-08-05
 */
@RestController
@RequestMapping("/userStatisticsDayLog")
public class UserStatisticsDayLogController {

    @Resource
    private UserStatisticsDayLogService userStatisticsDayLogService;
    @Resource
    private UserStatisticsInfoScheduledTask updateUserInfoTask;
    @Resource
    private UserStatisticsInfoService userStatisticsInfoService;

    @Operation(summary = "测试收益")
    @PostMapping(value = "/save")
    public R<Void> save(@RequestBody @Validated UserIncomeDayDTO userIncomeDayDTO) {
        userStatisticsDayLogService.saveUserRechargeInfo(userIncomeDayDTO);
        return R.ok();
    }

    @Operation(summary = "测试用户信息修正定时任务")
    @PostMapping(value = "/updateUserInfoTask")
    public R<Void> updateUserInfoTask() {
        updateUserInfoTask.updateUserStatisticsInfo(null);
        return R.ok();
    }


    @Operation(summary = "测试过期新增用户信息修正定时任务")
    @PostMapping(value = "/updateUserSubordinateInfoTask")
    public R<Void> updateUserSubordinateInfoTask(){
        updateUserInfoTask.updateUserSubordinateInfo(null);
        return R.ok();
    }

    @Operation(summary = "测试团用户数据修正定时任务")
    @PostMapping(value = "/updateUserTeamTask")
    public R<Void> updateUserTeamTask() {
        updateUserInfoTask.updateUserTeam(null);
        return R.ok();
    }

    @Operation(summary = "更新用户统计表中子级统计")
    @PostMapping(value = "/updateUserChildInfoTask")
    public R<Void> updateUserChildInfoTask(@RequestParam List<String> userIds) {
        List<UserStatisticsDayLog> list=userStatisticsDayLogService.getTodayToType(null);
        List<UserStatisticsDayLog> userList=list.stream().filter(a-> a.getType() ==0).toList();
        Map<Long,UserStatisticsDayLog> teamMap=list.stream().filter(a-> a.getType() ==2).collect(Collectors.toMap(UserStatisticsDayLog::getRelevanceId, Function.identity()));
        LambdaQueryWrapper<UserStatisticsInfo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(UserStatisticsInfo::getUserId,userIds.stream().map(Long::valueOf).toList());
        List<UserStatisticsInfo> userStatisticsInfos= userStatisticsInfoService.list(queryWrapper);

        updateUserInfoTask.updateUserChildInfo(userStatisticsInfos,userList,teamMap);
        userStatisticsDayLogService.updateBatchById(teamMap.values());
        return R.ok();
    }
}
